package com.luo.chengrui.module.crbpm.controller.admin.orgdefinition;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.crbpm.controller.admin.orgdefinition.vo.*;
import com.luo.chengrui.module.crbpm.dal.dataobject.orgdefinition.BpmOrgDefinitionDO;
import com.luo.chengrui.module.crbpm.convert.orgdefinition.BpmOrgDefinitionConvert;
import com.luo.chengrui.module.crbpm.service.orgdefinition.BpmOrgDefinitionService;

@Tag(name = "管理后台 - 组织机构流程模型定义")
@RestController
@RequestMapping("/crbpm/bpm-org-definition")
@Validated
public class BpmOrgDefinitionController {

    @Resource
    private BpmOrgDefinitionService bpmOrgDefinitionService;

    @PostMapping("/create")
    @Operation(summary = "创建组织机构流程模型定义")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:create')")
    public CommonResult<String> createBpmOrgDefinition(@Valid @RequestBody BpmOrgDefinitionCreateReqVO createReqVO) {
        return success(bpmOrgDefinitionService.createBpmOrgDefinition(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织机构流程模型定义")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:update')")
    public CommonResult<Boolean> updateBpmOrgDefinition(@Valid @RequestBody BpmOrgDefinitionUpdateReqVO updateReqVO) {
        bpmOrgDefinitionService.updateBpmOrgDefinition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织机构流程模型定义")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:delete')")
    public CommonResult<Boolean> deleteBpmOrgDefinition(@RequestParam("id") String id) {
        bpmOrgDefinitionService.deleteBpmOrgDefinition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织机构流程模型定义")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:query')")
    public CommonResult<BpmOrgDefinitionRespVO> getBpmOrgDefinition(@RequestParam("id") String id) {
        BpmOrgDefinitionDO bpmOrgDefinition = bpmOrgDefinitionService.getBpmOrgDefinition(id);
        return success(BpmOrgDefinitionConvert.INSTANCE.convert(bpmOrgDefinition));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织机构流程模型定义列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:query')")
    public CommonResult<List<BpmOrgDefinitionRespVO>> getBpmOrgDefinitionList(@RequestParam("ids") Collection<String> ids) {
        List<BpmOrgDefinitionDO> list = bpmOrgDefinitionService.getBpmOrgDefinitionList(ids);
        return success(BpmOrgDefinitionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织机构流程模型定义分页")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:query')")
    public CommonResult<PageResult<BpmOrgDefinitionRespVO>> getBpmOrgDefinitionPage(@Valid BpmOrgDefinitionPageReqVO pageVO) {
        PageResult<BpmOrgDefinitionDO> pageResult = bpmOrgDefinitionService.getBpmOrgDefinitionPage(pageVO);
        return success(BpmOrgDefinitionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织机构流程模型定义 Excel")
    @PreAuthorize("@ss.hasPermission('crbpm:bpm-org-definition:export')")
    @OperateLog(type = EXPORT)
    public void exportBpmOrgDefinitionExcel(@Valid BpmOrgDefinitionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BpmOrgDefinitionDO> list = bpmOrgDefinitionService.getBpmOrgDefinitionList(exportReqVO);
        // 导出 Excel
        List<BpmOrgDefinitionExcelVO> datas = BpmOrgDefinitionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织机构流程模型定义.xls", "数据", BpmOrgDefinitionExcelVO.class, datas);
    }

}
